package com.hbnrtech.sip.gb28181.handler;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import javax.sip.*;
import javax.sip.header.*;
import javax.sip.message.MessageFactory;
import javax.sip.message.Request;
import javax.sip.message.Response;
import java.text.ParseException;
import java.util.UUID;

/**
 * @author liuduo
 * @date 2025-04-21 17:25
 */
@Slf4j
@Data
public abstract class SipHandler {

    @Autowired
    private MessageFactory messageFactory;

    // 发送通用错误响应
    public void sendResponse(int statusCode, Request request, ServerTransaction st, String reasonPhrase)
            throws ParseException, SipException, InvalidArgumentException {
        Response response;
        response = messageFactory.createResponse(statusCode, request);
        if (reasonPhrase != null && !reasonPhrase.isEmpty()) {
            response.setReasonPhrase(reasonPhrase);
        }
        // To tag 必须有
        ToHeader toHeader = (ToHeader) response.getHeader(ToHeader.NAME);
        if (toHeader.getTag() == null) {
            // 从请求的 From 头获取 Tag，如果请求规范的话 To 头应该也有
            FromHeader fromHeader = (FromHeader) request.getHeader(FromHeader.NAME);
            if (fromHeader.getTag() != null) {
                toHeader.setTag(fromHeader.getTag());
            } else {
                toHeader.setTag(UUID.randomUUID().toString()); // 生成一个
            }
        }

        st.sendResponse(response);
        log.debug("Sent {} {} response", statusCode, reasonPhrase);
    }

    public ViaHeader getViaHeader(RequestEvent requestEvent) {
        Request request = requestEvent.getRequest();
        return (ViaHeader) request.getHeader(ViaHeader.NAME);
    }

    public FromHeader getFromHeader(RequestEvent requestEvent) {
        Request request = requestEvent.getRequest();
        return (FromHeader) request.getHeader(FromHeader.NAME);
    }

    public ToHeader getToHeader(RequestEvent requestEvent) {
        Request request = requestEvent.getRequest();
        return (ToHeader) request.getHeader(ToHeader.NAME);
    }

    public ContactHeader getContactHeader(RequestEvent requestEvent) {
        Request request = requestEvent.getRequest();
        return (ContactHeader) request.getHeader(ContactHeader.NAME);
    }

    public ExpiresHeader getExpiresHeader(RequestEvent requestEvent) {
        Request request = requestEvent.getRequest();
        return request.getExpires();
    }

    public CallIdHeader getCallIdHeader(RequestEvent requestEvent) {
        Request request = requestEvent.getRequest();
        return (CallIdHeader) request.getHeader(CallIdHeader.NAME);
    }

    public CSeqHeader getCSeqHeader(RequestEvent requestEvent) {
        Request request = requestEvent.getRequest();
        return (CSeqHeader) request.getHeader(CSeqHeader.NAME);
    }

    public AuthorizationHeader getAuthorizationHeader(RequestEvent requestEvent) {
        Request request = requestEvent.getRequest();
        return (AuthorizationHeader) request.getHeader(AuthorizationHeader.NAME);
    }

    public ContentTypeHeader getContentTypeHeader(RequestEvent requestEvent) {
        Request request = requestEvent.getRequest();
        return (ContentTypeHeader) request.getHeader(ContentTypeHeader.NAME);
    }
}
